///////////////////////////////////////////////////////////
//  CChoicesView.cpp
//  Implementation of the Class CChoicesView
//  Created on:      21-Sep-2013 9:36:32 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CChoicesView.h"
#include "ControlCommand.h"
#include "stdafx.h"

using namespace UI;
using namespace UI::CMD;

CChoicesView::CChoicesView(IWindowDirector* director)
{
  this->m_type = IViewCtrl::CHOICESVIEW_T;
  this->m_parrentWnd = director;
  this->m_curSelChoice = -1;
  this->m_choices = NULL;

  // call initialize for create all UI.
  Initialize();
}

CChoicesView::~CChoicesView()
{

}

UI::IViewCtrl::ViewType CChoicesView::GetType() const 
{

	return  this->m_type;
}


void CChoicesView::Initialize()
{
  this->SetAttribute("x1", 0);
  this->SetAttribute("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "100%");
  this->SetAttribute("backgroundColour", CColour(92,92,92,130));


  // Create navigationbar and set right button name
  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Select choice")
    .Set("leftButtonCaption", "Cancle")
    .Set("rightButtonCaption", "Done")
    .Set("x1",      0)
    .Set("y1",      "50%")
    .Set("width",   "100%")
    .Set("height",  "10%"));

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CChoicesView::OnClickedNavigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  // Create List box
  this->m_listBox = CreateListBox(CAttributes()
    .Set("name",    "Listbox sample")
    .Set("x1",      0)
    .Set("y1",      "60%")
    .Set("width",   "100%")
    .Set("height",  "43%"));

  // Add list box in view
  this->AddChild(this->m_listBox);
}


void CChoicesView::Refresh()
{
  // Create List box item
  CStringArray listBoxItems;
  int countChoices = this->m_choices->CountChoices();
  for(int index = 0; index < countChoices ; index++)
  {
    listBoxItems.AddString(CString(this->m_choices->GetChoice(index).c_str()));
  }

  this->m_listBox->SetAttribute("listBoxItems", listBoxItems);
  this->m_listBox->SetAttribute("selected", this->m_curSelChoice);
}


void CChoicesView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
}


void CChoicesView::OnClickedCancel()
{

}


void CChoicesView::OnClickedOK()
{

}


void CChoicesView::SetChoices(IChoices* choices, int curSel)
{
  this->m_choices = choices;
  this->m_curSelChoice = curSel;
}

bool UI::CChoicesView::OnClickedNavigationBar( CNavigationBar *naviBar, int button )
{
  switch(button)
  {
  case S3E_NUI_NAVIGATION_BAR_BUTTON_LEFT:
    {
      this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATECHECKLISTVIEW_T);
    }
    break;
  case S3E_NUI_NAVIGATION_BAR_BUTTON_RIGHT:
    {
      ControlCommand::GetInstance()->OnSave(ControlCommand::CHOICE_CTRL);
      this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATECHECKLISTVIEW_T);
    }
    break;
  default: break;
  }
  return true;
}

int UI::CChoicesView::GetChoice()
{
  int iSelect = 0;
  this->m_listBox->GetAttribute("selected", iSelect);
  return iSelect;
}
